<?php
/**
 * 用户管理
 * @author SongKejing 2012.12.26
 */
class UserAction extends BaseAction{
	
	/**
	 * 用户登录
	 */
	public function userLogin(){
		//判断用户是否登录
		if(isset($_SESSION['uid'])){
			$uid = $_SESSION['uid'];
			$user = new UserModel();
			$user->getUserNavigation($uid);
			//获取所有订单信息
			$allOrderInfo = $user->getAllOrderInfo();
			//获取当天订单信息
			$todayOrderInfo = $user->getTodayOrderInfo();
			$this->assign('allOrderCount1', isset($allOrderInfo[1]['all_count']) ? $allOrderInfo[1]['all_count'] : 0);
			$this->assign('allOrderCount2', isset($allOrderInfo[2]['all_count']) ? $allOrderInfo[2]['all_count'] : 0);
			$this->assign('todayOrderCount1', isset($todayOrderInfo[1]['all_count']) ? $todayOrderInfo[1]['all_count'] : 0);
			$this->assign('todayOrderCount2', isset($todayOrderInfo[2]['all_count']) ? $todayOrderInfo[2]['all_count'] : 0);
			$this->assign('today', date('Y年m月d日'));
			$this->display();
			return;
		}
		$name = isset($_POST['name']) ? $_POST['name'] : NULL;
		$psw = isset($_POST['psw']) ? $_POST['psw'] : NULL;
		$true_psw = isset($_POST['true_psw']) ? $_POST['true_psw'] : NULL;		
		$this->assign('name', $name);
		$this->assign('psw', $psw);
		if (empty($name) || empty($true_psw)){
			$this->assign('errorMsg', '用户名或密码不可为空！');
			$this->display('Public:login');
		}else{
			$user = new UserModel();
			$login_result = $user->userLogin($name, $true_psw);
			if ($login_result==FALSE){
				$this->assign('errorMsg', '用户名或密码错误，请重试！');
				$this->display('Public:login');
			}else{
				$uid = $_SESSION['uid'];
				$user->getUserNavigation($uid);
				//获取所有订单信息
				$allOrderInfo = $user->getAllOrderInfo();
				//获取当天订单信息
				$todayOrderInfo = $user->getTodayOrderInfo();
				$this->assign('allOrderCount1', isset($allOrderInfo[1]['all_count']) ? $allOrderInfo[1]['all_count'] : 0);
				$this->assign('allOrderCount2', isset($allOrderInfo[2]['all_count']) ? $allOrderInfo[2]['all_count'] : 0);
				$this->assign('todayOrderCount1', isset($todayOrderInfo[1]['all_count']) ? $todayOrderInfo[1]['all_count'] : 0);
				$this->assign('todayOrderCount2', isset($todayOrderInfo[2]['all_count']) ? $todayOrderInfo[2]['all_count'] : 0);
				$this->assign('today', date('Y年m月d日'));
				$this->display();
			}		
		}
	}
	
	/**
	 * 跳转到用户首页
	 */
	public function gotoHomePage(){
		$user = new UserModel();
		//获取所有订单信息
		$allOrderInfo = $user->getAllOrderInfo();
		//获取当天订单信息
		$todayOrderInfo = $user->getTodayOrderInfo();
		$this->assign('allOrderCount1', isset($allOrderInfo[1]['all_count']) ? $allOrderInfo[1]['all_count'] : 0);
		$this->assign('allOrderCount2', isset($allOrderInfo[2]['all_count']) ? $allOrderInfo[2]['all_count'] : 0);
		$this->assign('todayOrderCount1', isset($todayOrderInfo[1]['all_count']) ? $todayOrderInfo[1]['all_count'] : 0);
		$this->assign('todayOrderCount2', isset($todayOrderInfo[2]['all_count']) ? $todayOrderInfo[2]['all_count'] : 0);
		$this->assign('today', date('Y年m月d日'));
		$this->display('User:userLogin');
	}
	
	/**
	 * 获取用户信息
	 */
	public function getUserInfo(){
		$user = new UserModel();
		$userInfo = $user->getUserInfo();
		$roles = $user->getUserRoles();
		$this->assign('roles', $roles);
		$this->assign('name', $userInfo[0]['name']);
		$this->assign('true_name', $userInfo[0]['true_name']);
		$this->assign('email', $userInfo[0]['email']);
		$this->assign('remark', $userInfo[0]['remark']);
		$this->display();
	}
	
	/**
	 * 更新用户资料
	 */
	public function updateUserInfo(){
		$user = new UserModel();
//		if(!$user->autoCheckToken($_POST)){
//			//无效请求
//			exit('无效请求');
//		}
		$name = $_POST['name'];
		$true_name = $_POST['true_name'];
		$email = $_POST['email'];
		$remark = $_POST['remark'];
		$new_psw = empty($_POST['true_psw']) ? '' : $_POST['true_psw'];
		$old_psw = empty($_POST['true_old_psw']) ? '' : $_POST['true_old_psw'];
		//角色赋值
		$roles = array(3);
		if(isset($_POST['role1'])){
			array_push($roles, 1);
		}
		if(isset($_POST['role2'])){
			array_push($roles, 2);
		}
		$result = $user->updateUserInfo($name, $true_name, $email, $remark, $new_psw, $old_psw, $roles);
		$roles = $user->getUserRoles();
		$this->assign('roles', $roles);
		$this->assign('name', $name);
		$this->assign('true_name', $true_name);
		$this->assign('email', $email);
		$this->assign('remark', $remark);
		$this->assign('msg', $result['msg']);
		$this->display('User:getUserInfo');
	}
	
	/**
	 * 新建用户
	 */
	public function newUser(){	
		if (isset($_GET['isNew'])){
			$this->display();
			return;
		}
		$name = $_POST['name'];
		$true_name = $_POST['true_name'];
		$email = $_POST['email'];
		$remark = $_POST['remark'];
		$psw = empty($_POST['true_psw']) ? '' : $_POST['true_psw'];
		//角色赋值
		$roles = array(3);
		if(isset($_POST['role1'])){
			array_push($roles, 1);
		}
		if(isset($_POST['role2'])){
			array_push($roles, 2);
		}
		$user = new UserModel();
		$result = $user->newUser($name, $true_name, $psw, $email, $remark, $roles);
		$roles = $user->getUserRoles($result['uid']);
		$this->assign('roles', $roles);
		$this->assign('name', $name);
		$this->assign('true_name', $true_name);
		$this->assign('email', $email);
		$this->assign('remark', $remark);
		$this->assign('msg', $result['msg']);
		$this->display();
	}
	
	/**
	 * 退出登录
	 */
	public function logout(){
		Session::destroy();
		$this->display('Public:login');
	}
	
	/**
	 * 获取所有的用户
	 */
	public function getUsers(){
		$user = new UserModel();
		$current_page = isset($_GET['current_page']) ? $_GET['current_page'] : 1;
		$page_step = isset($_GET['page_step']) ? $_GET['page_step'] : 12;
		$page_count = isset($_GET['page_count']) ? $_GET['page_count'] : 0;
		$users = $user->getAllUsers($current_page, $page_step, $page_count);
		$this->assign('users', $users);
		$this->display();
	}
	
	/**
	 * 更新用户资料
	 */
	public function modifyUserInfo(){
		$name = $_POST['name'];
		$true_name = $_POST['true_name'];
		$email = $_POST['email'];
		$remark = $_POST['remark'];
		$uid = $_POST['uid'];
		$user = new UserModel();
		$roles = $user->getUserRoles($uid);		
		$this->assign('uid', $uid);
		$this->assign('roles', $roles);
		$this->assign('name', $name);
		$this->assign('true_name', $true_name);
		$this->assign('email', $email);
		$this->assign('remark', $remark);
		if($_POST['deleteValue']=='true'){
			$result = $user->deleteModifyUserInfo($uid);
			$this->assign('msg', $result['msg']);
			$this->assign('deleteValue', true);
			$this->display('User:getModifyUserInfo');
		}else{			
			$psw = empty($_POST['true_psw']) ? '' : $_POST['true_psw'];
			//角色赋值
			$roles = array(3);
			if(isset($_POST['role1'])){
				array_push($roles, 1);
			}
			if(isset($_POST['role2'])){
				array_push($roles, 2);
			}
			$result = $user->updateModifyUserInfo($uid, $name, $true_name, $email, $remark, $psw, $roles);
			$roles = $user->getUserRoles($uid);
			$this->assign('roles', $roles);
			$this->assign('msg', $result['msg']);
			$this->display('User:getModifyUserInfo');
		}
	}
	
	/**
	 * 获取修改用户的资料
	 */
	public function getModifyUserInfo(){
		$user = new UserModel();
		$uid = $_GET['uid'];
		$userInfo = $user->getUserInfo($uid);
		$roles = $user->getUserRoles($uid);
		$this->assign('uid', $uid);
		$this->assign('roles', $roles);
		$this->assign('name', $userInfo[0]['name']);
		$this->assign('true_name', $userInfo[0]['true_name']);
		$this->assign('email', $userInfo[0]['email']);
		$this->assign('remark', $userInfo[0]['remark']);
		$this->display();
	}
		
}
?>